<?php
/**
 *@author Connor <caokang@foxmail.com>
 *@todo
 *@link www.zeevin.com
 */
class PicController extends BaseController{
	private $upload_dir;

	public function init(){
		parent::init();
		$this->upload_dir = Yii::app()->basePath.'/../attachment/'.date('Ym').'/';
	}

	public function actionIndex(){
		$rs = Pic::getList();
		$this->render('index',array('data'=>$rs));
	}

	public function actionMult(){
		$this->render('mult');
	}

	public function actionUpload(){
		$model = new Pic('insert');
		$count = $model->countBySql('select count(1) from user_pics where uid=?',array($this->uid));
		if($_POST['Pic']){
			if($count>=20){
				$msg['status']=1;
				$msg['desc']='图片上传失败，您的图片数量已经达到20个。';

			}else{
			$inst = CUploadedFile::getInstance($model, 'picpath');
			$hash_name = $this->uid.'-'.substr(time(),6).rand(100,999).'.'.$inst->extensionName;
			$model->title = $_POST['Pic']['title'];
			$model->uid = $this->uid;
			$model->ctime = date('Y-m-d H:i:s');
			$model->hash_name = $hash_name;

			if($model->save()){
				if(!empty($inst)){
					file_exists($this->upload_dir)?null:mkdir($this->upload_dir);

					if($inst->saveAs($this->upload_dir.$hash_name)){
						$link=CHtml::link(CHtml::encode(mb_substr($model->title,0,16)), 'javascript:void(0)',array('tag'=>$model->id,'class'=>'photos_list_zee'));
						Feed::addfeed(Feed::TYPE_PIC_MSG, $link);
						$msg['status']=0;
						$msg['desc']='图片上传成功';
					}else{
						$model->delete();
						$msg['status']=1;
						$msg['desc']='图片上传失败：'.$inst->error;

					}
				}
			}else{
				$msg['status']=1;

				}
			}
		}
		$this->render('upload',array('model'=>$model,'msg'=>$msg));
	}

		// 上传文件
	public function actionUpfile($wid=''){
		$attach=new Attach();
		// is_numeric($_GET['tid'])?null:exit('类别错误');
		if(!empty($_FILES)){
			$session_name=session_name();
			if(!isset($_POST[$session_name])){
				exit();
			}
			session_id($_POST[$session_name]);
			session_start();
			
			$targetPath=Yii::app()->basePath.'/../attachment/machtinfo/'.date('Ym').'/';
			file_exists($targetPath)?null:mkdir($targetPath);
			
			$tempFile=$_FILES['Filedata']['tmp_name'];
			
			// Validate the file type
			$fileTypes=array(
				'jpg',
				'jpeg',
				'gif',
				'png',
				'bmp'
			); // File extensions
			$fileParts=pathinfo($_FILES['Filedata']['name']);
			$name=$_POST['type'].'-'.time().rand(100,999).'.'.$fileParts['extension'];
			$targetFile=rtrim($targetPath,'/').'/'.$name;
			
			if(in_array(strtolower($fileParts['extension']),$fileTypes)&&getimagesize($tempFile)){
				if(move_uploaded_file($tempFile,$targetFile)){
					$attach->scenario='create';
					$attach->attributes=array(
						'wf_id'=>$_POST['wid'],
						'name'=>$name,
						'type'=>$_POST['type'],
						'recordtime'=>date('Ym'),
						'creater'=>Yii::app()->user->id,
						);
					$attach->save();
				}
				echo 1; // success
			}else{
				echo 2; // Invalid file type.
			}
		}
	}

	public function actionDel(){
		if(Yii::app()->request->isAjaxRequest){
			if(Pic::del($_POST['id'])){
				$msg['status']=0;
			}else{
				$msg['status']=1;
				$msg['desc']='图片删除失败！';
			}
			echo json_encode($msg);
		}
	}
}